home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / ham / sattrk31.tgz / sattrack-3.1.tar / SatTrack / src / sattrack / sattime.c < prev    next >
C/C++ Source or Header  |  1995-03-16  |  20KB  |  600 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*  Title       : sattime.c                                                   */
  4. /*  Author      : Manfred Bester                                              */
  5. /*  Date        : 04Dec91                                                     */
  6. /*  Last change : 15Mar95                                                     */
  7. /*                                                                            */
  8. /*  Synopsis    : This function block contains auxiliary routines dealing     */
  9. /*                with time and date.                                         */
  10. /*                                                                            */
  11. /*                                                                            */
  12. /*  SatTrack is Copyright (c) 1992, 1993, 1994, 1995 by Manfred Bester.       */
  13. /*  All Rights Reserved.                                                      */
  14. /*                                                                            */
  15. /*  Permission to use, copy, and distribute SatTrack and its documentation    */
  16. /*  in its entirety for educational, research and non-profit purposes,        */
  17. /*  without fee, and without a written agreement is hereby granted, provided  */
  18. /*  that the above copyright notice and the following three paragraphs appear */
  19. /*  in all copies. SatTrack may be modified for personal purposes, but        */
  20. /*  modified versions may NOT be distributed without prior consent of the     */
  21. /*  author.                                                                   */
  22. /*                                                                            */
  23. /*  Permission to incorporate this software into commercial products may be   */
  24. /*  obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,    */
  25. /*  Berkeley, CA 94709, USA. Note that distributing SatTrack 'bundled' in     */
  26. /*  with ANY product is considered to be a 'commercial purpose'.              */
  27. /*                                                                            */
  28. /*  IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, */
  29. /*  SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF   */
  30. /*  THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED  */
  31. /*  OF THE POSSIBILITY OF SUCH DAMAGE.                                        */
  32. /*                                                                            */
  33. /*  THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT      */
  34. /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A   */
  35. /*  PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"      */
  36. /*  BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, */
  37. /*  UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                  */
  38. /*                                                                            */
  39. /******************************************************************************/
  40.  
  41. #include <stdio.h>
  42. #include <math.h>
  43. #include <string.h>
  44. #include <ctype.h>                                  /* needed for system time */
  45. #include <sys/types.h>                              /* needed for system time */
  46. #include <sys/time.h>                               /* needed for sleep timer */
  47. #include <signal.h>                                 /* needed for sleep timer */
  48. #include <memory.h>                                 /* needed for sleep timer */
  49.  
  50. #ifndef TIME                                        /* NOT included on Sun-3  */
  51. #include <time.h>                                   /* with SunOS 4.x.x       */
  52. #endif
  53.  
  54. #ifndef STDLIB                                      /* NOT included on Sun-4  */
  55. #include <stdlib.h>                                 /* with SunOS 4.x.x       */
  56. #endif
  57.  
  58. #include "satglobalsx.h"
  59. #include "sattrack.h"
  60.  
  61. /******************************************************************************/
  62. /*                                                                            */
  63. /* global fields                                                              */
  64. /*                                                                            */
  65. /******************************************************************************/
  66.  
  67. int monthDays[]  = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
  68. int noLeapYear[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  69. int leapYear[]   = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
  70.  
  71. /******************************************************************************/
  72. /*                                                                            */
  73. /* getUnixTime: reads Unix system time                                        */
  74. /*                                                                            */
  75. /******************************************************************************/
  76.  
  77. void getUnixTime(day,month,year,yday,hour,min,sec)
  78.  
  79. int *day, *month, *year, *yday, *hour, *min, *sec;
  80.  
  81. {
  82.     char   timeString[80];
  83.  
  84.     time_t timeofday;
  85.     struct tm *tm;
  86.  
  87.     time(&timeofday);
  88.     tm = gmtime(&timeofday);
  89.     strncpy(timeString,asctime(tm),16);
  90.     timeString[16] = '\0';
  91.  
  92.     *day   = tm->tm_mday;
  93.     *month = tm->tm_mon + 1;
  94.     *year  = tm->tm_year;
  95.     *yday  = tm->tm_yday + 1;
  96.     *hour  = tm->tm_hour;
  97.     *min   = tm->tm_min;
  98.     *sec   = tm->tm_sec;
  99.  
  100.     return;
  101. }
  102.  
  103. /******************************************************************************/
  104. /*                                                                            */
  105. /* getSiderealTime: calculates sidereal time                                  */
  106. /*                  for references see:                                       */
  107. /*                  "The Astronomical Almanac", 1986, page B6, B7 and L2      */
  108. /*                  "The Astronomical Almanac", 1984, page S16                */
  109. /*                                                                            */
  110. /*                  gmsTime    = Greenwich mean sidereal time                 */
  111. /*                  gasTime    = Greenwich apparent sidereal time             */
  112. /*                  lasTime    = local apparent sidereal time                 */
  113. /*                  equEquinox = equation of the equinoxes                    */
  114. /*                                                                            */
  115. /******************************************************************************/
  116.  
  117. void getSiderealTime(timeArg)
  118.  
  119. double timeArg;
  120.  
  121. {
  122.     double tu, tusq, tucb, utcInterval, julianDay, rss;
  123.  
  124.     utcInterval = modf(timeArg,&dummyd);                               /* [d] */
  125.  
  126.     julianDate  = timeArg + JULDAT1900 - 0.5;                          /* [d] */
  127.     julianDay   = julianDate - utcInterval;                            /* [d] */
  128.  
  129.     tu          = (julianDay - JULDAT2000) / JULCENT;                /* [jcy] */
  130.     tusq        = tu*tu;
  131.     tucb        = tu*tusq;
  132.  
  133.     gmsTime     = 24110.54841 + 8640184.812866*tu + 0.093104*tusq - 6.2e-6*tucb;
  134.  
  135.     tu          = (julianDate - JULDAT2000) / JULCENT;               /* [jcy] */
  136.     tusq        = tu*tu;
  137.     tucb        = tu*tusq;
  138.  
  139.     rss         = 1.002737909350795 + 5.9006e-11*tu - 5.9e-15*tusq;
  140.  
  141.     gmsTime    += rss * utcInterval * SPD;                           /* [sec] */
  142.     gmsTime    *= TWOPI / SPD;                                       /* [rad] */
  143.     gmsTime     = reduce(gmsTime,ZERO,TWOPI);                        /* [rad] */
  144.  
  145.     getNutation();
  146.  
  147.     gasTime    = gmsTime + equEquinox;                               /* [rad] */
  148.     gasTime    = reduce(gasTime,ZERO,TWOPI);                         /* [rad] */
  149.  
  150.     lasTime    = gasTime - siteLong;                                 /* [rad] */
  151.     lasTime    = reduce(lasTime,ZERO,TWOPI);
  152.  
  153.     return;
  154. }
  155.  
  156. /******************************************************************************/
  157. /*                                                                            */
  158. /* calendar: transforms the format of the calendar date                       */
  159. /*                                                                            */
  160. /******************************************************************************/
  161.  
  162. void calendar(iyear,yearday,pmonth,pday)
  163.  
  164. int iyear, yearday, *pmonth, *pday;
  165.  
  166. {
  167.     int month, day, check;
  168.  
  169.     month = 0;
  170.  
  171.     if (iyear%100 == 0 && iyear%400 != 0)
  172.           check = 1;
  173.     else
  174.           check = iyear%4;
  175.  
  176.     if (check == 0)
  177.     {
  178.         while (yearday > 0)
  179.         {
  180.              month++; 
  181.              yearday -= leapYear[month];
  182.         }
  183.  
  184.         day = yearday + leapYear[month];
  185.     } 
  186.  
  187.     else
  188.     {
  189.         while (yearday > 0)
  190.         {
  191.              month++;
  192.              yearday -= noLeapYear[month];
  193.         } 
  194.  
  195.         day = yearday + noLeapYear[month];
  196.     }
  197.  
  198.     *pmonth = month;
  199.     *pday   = day;
  200.  
  201.     return;
  202. }
  203.  
  204. /******************************************************************************/
  205. /*                                                                            */
  206. /* dayName: returns the name of the nth day                                   */
  207. /*                                                                            */
  208. /******************************************************************************/
  209.  
  210. char *dayName(dDay)
  211.  
  212. int dDay;
  213.  
  214. {
  215.     return((dDay < 0 || dDay > 6) ? "XXX" : dayNames[dDay]);
  216. }
  217.  
  218. /******************************************************************************/
  219. /*                                                                            */
  220. /* monthName: returns the name of the nth month                               */
  221. /*                                                                            */
  222. /******************************************************************************/
  223.  
  224. char *monthName(mMonth)
  225.  
  226. int mMonth;
  227.  
  228. {
  229.     return((mMonth < 1 || mMonth > 12) ? "XXX" : monthNames[mMonth-1]);
  230. }
  231.  
  232. /******************************************************************************/
  233. /*                                                                            */
  234. /* getDayNum: gets the day number for a given date. Jan. 1 of the reference   */
  235. /*            year is day 0. Note that the day number may be negative, if the */
  236. /*            sidereal reference is in the future.                            */
  237. /*                                                                            */
  238. /*            January 1, 1900 is day 0 valid from 1950 through 2049           */
  239. /*                                                                            */
  240. /******************************************************************************/
  241.  
  242. long getDayNum(gdnD,gdnM,gdnY)
  243.  
  244. int gdnD, gdnM, gdnY;
  245.  
  246. {
  247.     long result;
  248.     
  249.     if (gdnY < 50)                  /* allow 4 or 2 digit year specifications */
  250.         gdnY += 2000;
  251.     else
  252.         if (gdnY < 100)
  253.             gdnY += 1900;
  254.  
  255.     result = (long) ((((gdnY-1901)*1461) >> 2) + monthDays[gdnM-1] + gdnD+365);
  256.  
  257.     if (gdnY%4 == 0 && gdnM > 2)          /* correct day number for leap year */
  258.         result++;
  259.  
  260.     return(result);
  261. }
  262.  
  263. /******************************************************************************/
  264. /*                                                                            */
  265. /* getDate: gets the date from a given day number (see getDayNum)             */
  266. /*                                                                            */
  267. /******************************************************************************/
  268.  
  269. void getDate(dayNum,year,month,day,yearDay)
  270.  
  271. long dayNum;
  272. int  *year, *month, *day, *yearDay;
  273.  
  274. {
  275.     int gdLD, gdYD, gdD, gdM, gdY;
  276.  
  277.     gdY   = (int) ((double) (4 * dayNum) / 1461.0);
  278.     gdYD  = (int) (dayNum - (long) (365 + (((gdY - 1) * 1461) >> 2)));
  279.     gdY  += 1900;
  280.     gdLD  = (gdY%4 == 0 && gdYD > monthDays[2]) ? 1 : 0;
  281.     gdM   = 0;
  282.  
  283.     do
  284.     {
  285.         gdM++;
  286.     }
  287.     while (gdYD > monthDays[gdM] + gdLD);
  288.  
  289.     gdD = gdYD - monthDays[gdM-1];
  290.  
  291.     if (gdM > 2)
  292.         gdD -= gdLD;
  293.  
  294.     *year    = gdY;
  295.     *month   = gdM;
  296.     *day     = gdD;
  297.     *yearDay = gdYD;
  298.  
  299.     return;
  300. }    
  301.  
  302. /******************************************************************************/
  303. /*                                                                            */
  304. /* decodeDate: decodes date string                                            */
  305. /*                                                                            */
  306. /******************************************************************************/
  307.  
  308. int decodeDate(dateStr,pday,pmonth,pyear)
  309.  
  310. int  *pday, *pmonth, *pyear;
  311. char *dateStr;
  312.  
  313. {
  314.     int  i, error;
  315.     int  d = 0;
  316.     int  m = 0;
  317.     int y = 0;
  318.     char checkStr[10], monthStr[10];
  319.  
  320.     error = TRUE;
  321.  
  322.     if (strlen(dateStr) == 7)
  323.     {
  324.         d = -1;
  325.         m = -1;
  326.         y = -1;
  327.  
  328.         sscanf(dateStr,"%2d%3s%2d",&d,monthStr,&y);
  329.  
  330.         for (i = 1; i <= 12; i++)
  331.         {
  332.             sprintf(checkStr,"%s",monthName(i));
  333.             upperCase(checkStr);
  334.             upperCase(monthStr);
  335.  
  336.             if (!strcmp(checkStr,monthStr))
  337.             {
  338.                 m = i;
  339.  
  340.                 if (d >= 1 && d <= 31 && y >= 0 && y <= 99)
  341.                     error = FALSE;
  342.             }
  343.         }
  344.     }
  345.  
  346.     *pday   = d;
  347.     *pmonth = m;
  348.     *pyear  = y;
  349.  
  350.     return(error);
  351. }
  352.  
  353. /******************************************************************************/
  354. /*                                                                            */
  355. /* decodeTime: decodes time string                                            */
  356. /*                                                                            */
  357. /******************************************************************************/
  358.  
  359. int decodeTime(timeStr,phour,pmin,psec)
  360.  
  361. int  *phour, *pmin, *psec;
  362. char *timeStr;
  363.  
  364. {
  365.     int h, m, s, error;
  366.  
  367.     error = TRUE;
  368.  
  369.     h = -1;
  370.     m = -1;
  371.     s = -1;
  372.  
  373.     sscanf(timeStr,"%d:%d:%d",&h,&m,&s);
  374.  
  375.     if (h >= 0 && h <= 23 && m >= 0 && m <= 59 && s >= 0 && s <= 59)
  376.         error = FALSE;
  377.  
  378.     *phour = h;
  379.     *pmin  = m;
  380.     *psec  = s;
  381.  
  382.     return(error);
  383. }
  384.  
  385. /******************************************************************************/
  386. /*                                                                            */
  387. /* convertTime: converts format of time from decimal to integer               */
  388. /*                                                                            */
  389. /******************************************************************************/
  390.  
  391. void convertTime(time,days,hours,mins,secs)
  392.  
  393. double time;
  394. int    *days, *hours, *mins, *secs;
  395.  
  396. {
  397.     double timed, timeh, timem, times, timems;
  398.  
  399.     timed  = time;
  400.     timeh  = 24.0*modf(timed,&timed);
  401.     timem  = 60.0*modf(timeh,&timeh);
  402.     times  = 60.0*modf(timem,&timem);
  403.     timems = 1000.0*modf(times,×);
  404.  
  405.     if (timems >= 500)
  406.         times += 1.0;
  407.  
  408.     if (fabs(times - 60.0) < 5.0e-4)
  409.     {
  410.         times  = 0.0;
  411.         timem += 1.0;
  412.  
  413.         if (fabs(timem - 60.0) < 1.0e-4)
  414.         {
  415.             timem  = 0.0;
  416.             timeh += 1.0;
  417.  
  418.             if (fabs(timeh - 24.0) < 1.0e-4)
  419.             {
  420.                 timeh  = 0.0;
  421.                 timed += 1.0;
  422.             }
  423.         }
  424.     }
  425.  
  426.     *days  = (int) timed;
  427.     *hours = (int) timeh;
  428.     *mins  = (int) timem;
  429.     *secs  = (int) times;
  430.  
  431.     return;
  432. }
  433.  
  434. /******************************************************************************/
  435. /*                                                                            */
  436. /* printDate: prints date into output file                                    */
  437. /*                                                                            */
  438. /******************************************************************************/
  439.  
  440. void printDate(outFile,time)
  441.  
  442. double time;
  443. FILE   *outFile;
  444.  
  445. {
  446.     long dayNum;
  447.     int  day, month, year, yearDay;
  448.  
  449.     dayNum = (long) time;
  450.  
  451.     getDate(dayNum,&year,&month,&day,&yearDay);
  452.  
  453.     if (shortPredFlag && lastDayNum != dayNum)
  454.         fprintf(outFile,"\n");
  455.  
  456.     if (shortPredFlag && !firstLine && lastDayNum == dayNum)
  457.         fprintf(outFile,"            ");
  458.     else
  459.     {
  460.         fprintf(outFile,"%s ",dayName(dayNum%7));
  461.         fprintf(outFile," %02d%3s%02d",day,monthName(month),year%100);
  462.     }
  463.  
  464.     lastDayNum = dayNum;
  465.     return;
  466. }    
  467.  
  468. /******************************************************************************/
  469. /*                                                                            */
  470. /* printTime: prints time string                                              */
  471. /*                                                                            */
  472. /******************************************************************************/
  473.  
  474. void printTime(outFile,time)
  475.  
  476. double time;
  477. FILE   *outFile;
  478.  
  479. {
  480.     double dayTime;
  481.     long   dayNum;
  482.     int    hours, mins, secs;
  483.  
  484.     dayNum  = (long) time;
  485.     dayTime = time - (double) dayNum;
  486.  
  487.     convertTime(dayTime,&dummyi,&hours,&mins,&secs);
  488.  
  489.     if (time < 1000.0)                                /* for duration of pass */
  490.         hours += (int) dayNum * 24;
  491.  
  492.     fprintf(outFile,"%02d:%02d:%02d",hours,mins,secs);
  493.     return;
  494. }
  495.  
  496. /******************************************************************************/
  497. /*                                                                            */
  498. /* printMET: prints MET string into output file                               */
  499. /*                                                                            */
  500. /******************************************************************************/
  501.  
  502. void printMET(outFile,met)
  503.  
  504. double met;
  505. FILE   *outFile;
  506.  
  507. {
  508.     double metDayTime;
  509.     long   metDayNum;
  510.     int    negativeMET, metDay, metHour, metMin, metSec;
  511.  
  512.     metDayNum   = (long) met;
  513.     metDayTime  = met - (double) metDayNum;
  514.  
  515.     negativeMET = (met < 0.0) ? TRUE : FALSE;
  516.  
  517.     if (negativeMET)
  518.         met *= -1.0;
  519.  
  520.     convertTime(met,&metDay,&metHour,&metMin,&metSec);
  521.  
  522.     if (negativeMET)
  523.     {
  524.         if (metDay)
  525.             fprintf(outFile,"%3d/%02d:%02d:%02d",
  526.                              -metDay,metHour,metMin,metSec);
  527.         else
  528.             fprintf(outFile," -%d/%02d:%02d:%02d",
  529.                              -metDay,metHour,metMin,metSec);
  530.     }
  531.  
  532.     else
  533.         fprintf(outFile,"%3d/%02d:%02d:%02d",metDay,metHour,metMin,metSec);
  534.  
  535.     return;
  536. }
  537.  
  538. /******************************************************************************/
  539. /*                                                                            */
  540. /* milliSleep: sleeps for a specified time in milliseconds                    */
  541. /*                                                                            */
  542. /******************************************************************************/
  543.  
  544. static int timerDone;
  545.  
  546. void milliSleep(msec)
  547.  
  548. int msec;
  549.  
  550. {
  551.     long usec;
  552.     struct itimerval it;
  553.  
  554.     usec = (long) (msec * 1000);
  555.  
  556.     memset(&it,0,sizeof(it));
  557.     it.it_value.tv_usec = usec;
  558.     setitimer(ITIMER_REAL,&it,(struct itimerval *)0);
  559.  
  560.     signal(SIGALRM,onAlarm);
  561.  
  562.     HOLDSIG;                             /* block timer so that ALRM does not */
  563.                                          /* occur between 'if (timerDone)'    */
  564.                                          /* and calling PAUSESIG              */
  565.  
  566.     timerDone = FALSE;
  567.  
  568.     while (TRUE)
  569.     {
  570.         if (timerDone)
  571.             break;
  572.         else
  573.             PAUSESIG;                    /* this is the sleep function        */
  574.     }
  575.  
  576.     RELEASESIG;                          /* turn ALRM blocking off again      */
  577.  
  578.     signal(SIGALRM,SIG_DFL);
  579.     return;
  580. }
  581.  
  582. /******************************************************************************/
  583. /*                                                                            */
  584. /* onAlarm: turns off timer when expired                                      */
  585. /*                                                                            */
  586. /******************************************************************************/
  587.  
  588. void onAlarm()
  589.  
  590. {
  591.     timerDone = TRUE;
  592.     return;
  593. }
  594.  
  595. /******************************************************************************/
  596. /*                                                                            */
  597. /* End of function block sattime.c                                            */
  598. /*                                                                            */
  599. /******************************************************************************/
  600.